package lerry.nape.utils;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import lerry.display.StageInfo;
import lerry.interaction.Interaction;
import nape.constraint.PivotJoint;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyList;
import nape.space.Space;
import nape.util.Debug;
import nape.util.ShapeDebug;





#if flash
import nape.util.BitmapDebug;
#else 
#end

/**
 * ...
 * @author lerry
 */
class Option{

	
	public var down:Dynamic->Void;
	public var up:Dynamic->Void;
	public var update:Dynamic->Void;
	
	public var useGraphic:Bool;
	public var dragEnabled(null, set_dragEnabled):Bool;
	public var useDebug(null, set_useDebug):Bool;
	
	

	var debug:Debug;
	var hand:PivotJoint;
	var source:DisplayObjectContainer;
	var space:Space;
	
	public function new(space:Space,source:DisplayObjectContainer) {
		this.source = source;
		this.space = space;
		
		this.useGraphic = true;
		this.useDebug = true;
		this.dragEnabled = true;
		
		this.down = null;
		this.up = null;
		this.update = null;
		
		
	}
		
	//设置重力
	public function setGravity(x:Float, y:Float):Void {
		space.gravity.setxy(x, y);
	}
	
	function set_dragEnabled(value:Bool):Bool {
		if (value) {
			if (hand == null) {
				hand = new PivotJoint(space.world, null, Vec2.get(), Vec2.get());
				hand.space = space;
				hand.active = false;
				hand.stiff = false;
			}
			#if mobile
			Interaction.TouchBegin(NapeEngine.source, onMouseDown  );
			Interaction.TouchBegin(NapeEngine.source,onMouseUp );
			#else
			Interaction.MouseDown(NapeEngine.source, onMouseDown  );
			Interaction.MouseUp(NapeEngine.source, onMouseUp );
			#end
			
		}else {
			#if mobile
			Interaction.TouchBegin(NapeEngine.source, null  );
			Interaction.TouchBegin(NapeEngine.source,null );
			#else
			Interaction.MouseDown(NapeEngine.source, null  );
			Interaction.MouseUp(NapeEngine.source, null );
			#end
		}
		return value;
	}
	
	function onMouseMove(e:Dynamic):Void {
		hand.anchor1.setxy(e.stageX, e.stageY);
	}
	function set_useDebug(value:Bool):Bool {
		if (value) {
			if (debug == null) {
				#if flash
				debug = new BitmapDebug(Std.int(StageInfo.STAGE_WIDTH), Std.int(StageInfo.STAGE_HEIGHT), 0x000000, true);
				#else 
				debug = new ShapeDebug(Std.int(StageInfo.STAGE_WIDTH), Std.int(StageInfo.STAGE_HEIGHT), 0x000000);
				#end
				source.addChild(debug.display);	
				debug.drawConstraints = true;
			}

		}else {
			if (debug != null) {
				source.removeChild(debug.display);
				debug = null;
			}
		}
		return value;
	}
	function onMouseDown(e:Dynamic):Void {
		#if mobile
		Interaction.TouchMove(NapeEngine.source, onMouseMove);
		#else
		Interaction.MouseMove(NapeEngine.source, onMouseMove);
		#end
		var mp:Vec2 = Vec2.get(e.stageX, e.stageY);
		var bodies:BodyList = space.bodiesUnderPoint(mp);
		bodies.foreach(function(b:Body):Void {
			if (b.isDynamic()) {
				hand.body2 = b;
				hand.anchor1.set(mp);
				hand.anchor2 = b.worldPointToLocal(mp);
				hand.active = true;
			}
		});
		if (this.down!=null) this.down(e);
	}
	function onMouseUp(e:Dynamic):Void {
		#if mobile
		Interaction.TouchMove(NapeEngine.source, null);
		#else
		Interaction.MouseMove(NapeEngine.source, null);
		#end
		hand.active = false;
		if (this.up!=null) this.up(e);
	}
	public function updateDebug():Void {
		if (debug != null) {
			debug.clear();
			debug.draw(space);
			debug.flush();
		}
	}
	public function updateGraphic():Void {
		if (!useGraphic) return;
		for (body in space.bodies) {
			var graphic:Null<DisplayObject> = body.userData.graphic;
			if (graphic == null) continue;
			var graphicOffset:Vec2 = body.userData.graphicOffset;
			if (graphicOffset == null) {
				graphicOffset = Vec2.get();
			}
			var position:Vec2 = body.localPointToWorld(graphicOffset);
			graphic.x = position.x;
			graphic.y = position.y;
			graphic.rotation = (body.rotation * 180/Math.PI) % 360;
			position.dispose();
		}
	}
}